import axios from "axios";
import {Toast} from 'vant';

//设置请求超时最大值
axios.defaults.timeout = 10000;

//设置 post请求头
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';


//展示vant组件的 加载动画
function startLoading(){
    Toast({
        message:'加载中',
        duration:1000,
        forbidClick:true
    })
}

//vant 加载完成动画
function endLoading(){
    Toast({
        message:'加载完成',
        duration:1000,
        forbidClick:true
    })
}
//vant 加载失败动画
function failLoading(){
    Toast({
        message:'加载失败',
        duration:1000,
        forbidClick:true
    })
}

// 请求拦截
axios.interceptors.request.use(
    (config) =>{
        startLoading();
        const token = store.state.token;
        //设置请求头
        config.headers.Authorization = token;
        endLoading();
        return config;
    },
    (error => {
        Promise.reject(error);
    })
)

//响应拦截
axios.interceptors.response.use(
    (response)=>{
        endLoading();
        return response;
    },
    error => {
        if (error.response.status){
            switch (error.response.status){
                case 2001:
                    Toast({
                       message:'用户未登录',
                       duration:1000,
                       forbidClick:true
                    });
                    setTimeout(()=>{
                        router.replace({
                            path: '/login',
                            query: {
                                redirect: router.currentRoute.fullPath
                            }
                        });
                    },1000);
                    break;
                case 2003:
                    Toast({
                        message:'密码错误',
                        duration:1000,
                        forbidClick:true
                    });
                    setTimeout(()=>{
                        router.replace({
                            path: '/login',
                            query: {
                                redirect: router.currentRoute.fullPath
                            }
                        });
                    },1000);
                    break;
                case 2007:
                    Toast({
                        message:'账号不存在',
                        duration:1000,
                        forbidClick:true
                    });
                    setTimeout(()=>{
                        router.replace({
                            path: '/login',
                            query: {
                                redirect: router.currentRoute.fullPath
                            }
                        });
                    },1000);
                    break;
                default:
                    Toast({
                        message: error.response.data.message,
                        duration: 1500,
                        forbidClick: true
                    });
            }
            return Promise.reject(error.response);
        }
    }
)

export default axios;